//
//  PickButton.m
//  Final
//
//  Created by Pibinuo on 15/12/8.
//  Copyright © 2015年 皮比诺. All rights reserved.
//

#import "PickButton.h"

@interface PickButton ()
@property (nonatomic, strong) UIButton *buttonOne;
@property (nonatomic, strong) UIButton *buttonTwo;
@property (nonatomic, strong) UIButton *buttonThree;
@property (nonatomic, strong) UIButton *buttonFour;
@property (nonatomic, strong) UIButton *buttonFive;
@property (nonatomic, strong) UIButton *buttonSix;
@property(nonatomic,strong)UIButton * saveBtoon;
@property(nonatomic,strong)UIView * yellowView;

@end


@implementation PickButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setButton];
    }
    return self;
}

-(void)setButton
{
    self.buttonOne = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOne.frame = CGRectMake(0, 0, self.frame.size.width / 2, self.frame.size.height - 5);
    self.buttonOne.tag = 10000;
    [self.buttonOne addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOne setTitle:@"糗友圈" forState:UIControlStateNormal];
    self.buttonOne.selected = YES;
    self.buttonOne.backgroundColor = [UIColor grayColor];
    [self addSubview:self.buttonOne];
    self.saveBtoon = self.buttonOne;
    
    self.buttonTwo = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonTwo.frame = CGRectMake(self.frame.size.width / 2, 0, self.frame.size.width / 2, self.frame.size.height - 5);
    [self.buttonTwo addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonTwo setTitle:@"新闻界" forState: UIControlStateNormal];
    self.buttonTwo.tag = 10001;
    
    self.buttonTwo.backgroundColor = [UIColor grayColor];
    [self addSubview:self.buttonTwo];
    
    
    //    self.buttonThree = [UIButton buttonWithType:UIButtonTypeCustom];
    //    self.buttonThree.frame = CGRectMake(self.frame.size.width / 3, 0, self.frame.size.width / 6, self.frame.size.height - 5);
    //    [self.buttonThree addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.buttonThree setTitle:@"界面三" forState: UIControlStateNormal];
    //    self.buttonThree.tag = 10002;
    //    self.buttonThree.backgroundColor = [UIColor grayColor];
    //    [self addSubview:self.buttonThree];
    //
    //    self.buttonFour = [UIButton buttonWithType:UIButtonTypeCustom];
    //    self.buttonFour.frame = CGRectMake(self.frame.size.width / 2, 0, self.frame.size.width / 6, self.frame.size.height - 5);
    //    [self.buttonFour addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.buttonFour setTitle:@"界面四" forState: UIControlStateNormal];
    //    self.buttonFour.tag = 10003;
    //    self.buttonFour.backgroundColor = [UIColor grayColor];
    //    [self addSubview:self.buttonFour];
    //
    //    self.buttonFive = [UIButton buttonWithType:UIButtonTypeCustom];
    //    self.buttonFive.frame = CGRectMake(self.frame.size.width *2 / 3, 0, self.frame.size.width / 6, self.frame.size.height - 5);
    //    [self.buttonFive addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.buttonFive setTitle:@"界面五" forState: UIControlStateNormal];
    //    self.buttonFive.tag = 10004;
    //    self.buttonFive.backgroundColor = [UIColor grayColor];
    //    [self addSubview:self.buttonFive];
    //
    //    self.buttonSix = [UIButton buttonWithType:UIButtonTypeCustom];
    //    self.buttonSix.frame = CGRectMake(self.frame.size.width * 5 / 6, 0, self.frame.size.width / 6, self.frame.size.height - 5);
    //    [self.buttonSix addTarget:self action:@selector(buttonClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    //    [self.buttonSix setTitle:@"界面六" forState:(UIControlStateNormal)];
    //    self.buttonSix.tag = 10005;
    //    self.buttonSix.backgroundColor = [UIColor grayColor];
    //    [self addSubview:self.buttonSix];
    //
    
    
    self.yellowView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 5, self.frame.size.width/2, 5)];
    self.yellowView.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.yellowView];
}

-(void)buttonClicked:(UIButton *)sender
{
    if (self.saveBtoon != sender) {
        self.saveBtoon.backgroundColor = [UIColor grayColor];
        self.saveBtoon.selected = !self.saveBtoon.selected;
        sender.backgroundColor = [UIColor brownColor];
        self.saveBtoon = sender;
        [UIView animateWithDuration:0.1 animations:^{
            self.yellowView.frame = CGRectMake(sender.frame.origin.x, self.frame.size.height - 5, self.frame.size.width/2, 5);
            
        }];
        self.myBlock(sender.tag - 10000);
        [[SoundHelper shareSound] playSound];
    }
    
}


-(void)setButtonClicked:(NSInteger)index
{
    UIButton * button = (UIButton *)[self viewWithTag:index + 10000];//
    [self buttonClicked:button];
}


@end
